package com.jrx.anytxn.transaction.timer.job.marketingTask.config;

import com.jrx.anytxn.customer.constant.CustomerConstant;
import com.jrx.anytxn.customer.entity.TlMarketingRecord;
import com.jrx.anytxn.ribbon.config.GrayVersionConfig;
import com.jrx.anytxn.transaction.timer.config.AnytxnBatchProperties;
import com.jrx.anytxn.transaction.timer.job.marketingTask.step.MarketingSendProcessor;
import com.jrx.anytxn.transaction.timer.job.marketingTask.step.MarketingSendWriter;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.support.SynchronizedItemStreamReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * 营销记录发送任务
 *
 * @author zcc
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class MarketingSendConfiguration {
    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;

    @Autowired
    private MarketingSendProcessor marketingSendProcessor;

    @Autowired
    private MarketingSendWriter marketingSendWriter;

    @Autowired
    DataSource dataSource;

    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    @Autowired
    public GrayVersionConfig grayVersionConfig;

    @Bean
    public Job marketingSendJob() {
        return jobBuilderFactory.get("marketingSendJob")
                .start(marketingSendStep())
                .build();
    }

    @Bean
    public Step marketingSendStep() {
        return stepBuilderFactory.get("marketingSendStep")
                .<TlMarketingRecord,TlMarketingRecord>chunk(anytxnBatchProperties.getQueryPaymentStatusConfig().getChunkLimit())//表示一批处理100条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(marketingSendReader())
                .processor(marketingSendProcessor)
                .writer(marketingSendWriter)
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getQueryPaymentStatusConfig().getThrottleLimit())//多线程
                .build();
    }

    @Bean
    @StepScope
    public SynchronizedItemStreamReader<TlMarketingRecord> marketingSendReader() {

        MyBatisCursorItemReader<TlMarketingRecord> cursorItemReader = new MyBatisCursorItemReader<>();
        cursorItemReader.setSaveState(false);
        cursorItemReader.setSqlSessionFactory(sqlSessionFactory);
        cursorItemReader.setQueryId("com.jrx.anytxn.customer.mapper.ext.ExtTlMarketingRecordMapper.selectBySumStatus");

        Map<String, Object> map = new HashMap<>();
        map.put("sumStatus", CustomerConstant.MARKET_SUM_STATUS_T);
        map.put("grayVersion", grayVersionConfig.getCurrentVersion());
        cursorItemReader.setParameterValues(map);
        //同步reader   线程安全
        SynchronizedItemStreamReader<TlMarketingRecord> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
        synchronizedItemStreamReader.setDelegate(cursorItemReader);
        return synchronizedItemStreamReader;
    }

}
